<?php
namespace Admin\Model;
use Think\Model;

class InstallModel extends Model{
	protected $autoCheckFields = false;
	//自动验证
	protected $_validate = array(
		array('db_host','require','数据库服务器不得为空！',self::EXISTS_VALIDATE),
		array('db_port','number','服务器端口必须是数字！',self::EXISTS_VALIDATE),
		array('db_name','require','数据库名不得为空！',self::EXISTS_VALIDATE),
		array('db_user','require','数据库用户名不得为空！',self::EXISTS_VALIDATE),
		array('db_pwd','require','数据库密码不得为空！',self::EXISTS_VALIDATE),
		array('db_prefix','require','数据表前缀不得为空！',self::EXISTS_VALIDATE),
		
		array('web_name','require','站点名称不得为空！',self::EXISTS_VALIDATE),
		array('order_time','number','防刷单间隔必须是数字！',self::EXISTS_VALIDATE),
		array('manage_enter','require','后台入口不得为空！',self::EXISTS_VALIDATE),
	);
	
	//第二步表单验证
	public function step2(){
		// if (I('post.pass') != preg_replace('/^\xEF\xBB\xBF/','',file_get_contents('1.html'))) return '安装密钥错误，请确保您的服务器处于联网状态，并检查是否输入正确！';
		$data = array(
			'db_host'=>I('post.db_host'),
			'db_port'=>I('post.db_port'),
			'db_name'=>I('post.db_name'),
			'db_user'=>I('post.db_user'),
			'db_pwd'=>I('post.db_pwd'),
			'db_prefix'=>I('post.db_prefix')
		);
		return $this->create($data) ? 1 : $this->getError();
	}

	//第三步表单验证
	public function step3(){
		$data = array(
			'web_name'=>I('post.web_name'),
			'order_time'=>I('post.order_time'),
			'manage_enter'=>I('post.manage_enter')
		);
		if ($this->create($data)){
			if (substr(I('post.manage_enter'),-4) != '.php') return '后台入口必须以.php结尾！';
			if (I('post.manage_enter',NULL,'strtolower') == 'admin.php') return '后台入口不得是admin.php！';
			if (is_file(ROOT_PATH.'/'.I('post.manage_enter'))) return '系统根目录中已存在'.I('post.manage_enter').'文件，请重新指定一个后台入口！';
			return 1;
		}else{
			return $this->getError();
		}
	}
}